import { findAllWire, addManyWire, delWire } from '@/apis/wireApi'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export default defineStore('wire', () => {
  const wires = ref<WireModel[]>([])

  async function all(args: Record<string, any>) {
    const { data } = await findAllWire(args)
    wires.value = data
  }

  async function add(args: Record<string, any>) {
    const { name, type, long, num, projId } = args
    const data = [] as WireModel[]
    for (let index = 0; index < num; index++) {
      data.push({
        name,
        type,
        long,
        projId,
      })
    }
    await addManyWire(data)
  }

  async function del(id: number) {
    await delWire(id)
  }

  return {
    wires,
    all,
    add,
    del,
  }
})
